<template>
  <div class="mod-unit">
    <el-form :inline="true" size="small" :model="dataForm">
      <el-form-item>
        <el-input
          v-model="dataForm.name"
          placeholder="名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="query()">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      size="mini"
      row-key="id"
      lazy
      border
      :load="load"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column type="index" align="center" label="编号" width="100" />
      <el-table-column
        prop="name"
        header-align="center"
        min-width="150"
        align="left"
        label="中文名称"
      >
      </el-table-column>

      <el-table-column
        prop="code"
        header-align="center"
        min-width="100"
        align="center"
        label="编码"
      >
      </el-table-column>

      <el-table-column
        prop="physicalName"
        header-align="center"
        min-width="150"
        align="center"
        label="物理名称"
      >
      </el-table-column>

      <el-table-column
        prop="dataState"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState === -1" size="small" type="danger"
            >禁用</el-tag
          >
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="orderNum"
        header-align="center"
        align="center"
        label="排序号"
      >
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        width="200"
        :show-overflow-tooltip="true"
        label="备注"
      >
      </el-table-column>

      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间"
      >
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <!-- <el-button v-if="isAuth('sys:menu:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button> -->
          <!-- <el-button v-if="isAuth('sys:menu:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button> -->

          <el-button
            type="text"
            v-if="scope.row.type != 5"
            size="small"
            @click="addHandle(scope.row, 1)"
            >新增</el-button
          >
          <el-button
            v-if="scope.row.type != 2"
            type="text"
            size="small"
            @click="updateHandle(scope.row, 2)"
            >修改</el-button
          >
          <el-button
            type="text"
            v-if="scope.row.type != 2"
            size="small"
            @click="deleteHandle(scope.row)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.dataState === -1"
            type="text"
            size="small"
            @click="enableHandle(scope.row)"
            >启用</el-button
          >
          <el-button
            v-if="scope.row.dataState === 0"
            type="text"
            size="small"
            @click="disableHandle(scope.row)"
            >冻结</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <db-add-or-update
      v-if="dbAddOrUpdateVisible"
      ref="dbAddOrUpdate"
      @refreshDataList="getDataList"
    ></db-add-or-update>

    <!-- 弹窗, 新增 / 修改 -->
    <table-add-or-update
      v-if="tableAddOrUpdateVisible"
      ref="tableAddOrUpdate"
      @refreshDataList="getDataList"
    ></table-add-or-update>

    <field-add-or-update
      v-if="fieldAddOrUpdateVisible"
      ref="fieldAddOrUpdate"
      @refreshDataList="getDataList"
    ></field-add-or-update>
  </div>
</template>


<script>
import tableAddOrUpdate from "./table-add-or-update";
import dbAddOrUpdate from "./db-add-or-update";
import fieldAddOrUpdate from "./field-add-or-update";
import {
  pageList,
  removeDb,
  removeTable,
  removeField,
  enableDb,
  enableTable,
  enableField,
  disableDb,
  disableTable,
  disableField,
} from "@/api/table";
import { treeDataTranslate } from "@/utils";

export default {
  data() {
    return {
      dataForm: {
        name: "",
      },
      dataList: [],
      dataListLoading: false,
      dbAddOrUpdateVisible: false,
      tableAddOrUpdateVisible: false,
      fieldAddOrUpdateVisible: false,
      dataListSelections: [],
    };
  },
  components: {
    tableAddOrUpdate,
    dbAddOrUpdate,
    fieldAddOrUpdate,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    query() {
      this.getDataList();
    },
    load(tree, treeNode, resolve) {
      this.loadTablelist(
        {
          type: tree.type,
          parentId: tree.id,
        },
        resolve
      );
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      pageList({ type: 1 }).then((res) => {
        if (res && res.code === 0) {
          this.dataList = treeDataTranslate(res.data, "id");
        }
        this.dataListLoading = false;
      });
    },
    loadTablelist(data, resolve) {
      this.dataListLoading = true;
      pageList(data).then((res) => {
        if (res && res.code === 0) {
          resolve(treeDataTranslate(res.data, "id"));
        }
        this.dataListLoading = false;
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addHandle(data, addChildren) {
      data.addChildren = addChildren;
      if (data.type == 2) {
        this.dbAddOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.dbAddOrUpdate.init(data);
        });
      } else if (data.type == 3) {
        this.tableAddOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.tableAddOrUpdate.init(data);
        });
      } else if (data.type == 4) {
        this.fieldAddOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.fieldAddOrUpdate.init(data);
        });
      }
    },
    updateHandle(data, addChildren) {
      data.addChildren = addChildren;
      if (data.type == 3) {
        this.dbAddOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.dbAddOrUpdate.init(data);
        });
      } else if (data.type == 4) {
        this.tableAddOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.tableAddOrUpdate.init(data);
        });
      } else if (data.type == 5) {
        this.fieldAddOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.fieldAddOrUpdate.init(data);
        });
      }
    },
    enableHandle(row) {
      this.$confirm(`确定进行启用操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let enableEvent = enableDb;
          if (row.type == 3) {
          } else if (row.type == 4) {
            enableEvent = enableTable;
          } else if (row.type == 5) {
            enableEvent = enableField;
          }
          enableEvent({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    disableHandle(row) {
      this.$confirm(`确定进行冻结操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let disableEvent = disableDb;
          if (row.type == 3) {
          } else if (row.type == 4) {
            disableEvent = disableTable;
          } else if (row.type == 5) {
            disableEvent = disableField;
          }
          disableEvent({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 删除
    deleteHandle(res) {
      this.$confirm(`确定对[${res.name}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let deleteEvent = removeDb;
          if (res.type == 3) {
          } else if (res.type == 4) {
            deleteEvent = removeTable;
          } else if (res.type == 5) {
            deleteEvent = removeField;
          }

          deleteEvent({ id: `${res.id}` }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>